<?php
class Home extends MY_Controller{
    var $menu_active = "home";
    var $column = 1;

    function Home()
    {
        parent::MY_Controller();
		$this->load->helper('form');
		$this->load->library('validation');
		$this->load->library('email');
		$this->load->helper('email');
		$this->load->plugin('phpmailer');
    }
  
    function index()
    {
        $this->GL();
    }
   
    function GL()
    {
        /*load view*/
        $this->uri = 'http://dichvusuanhadandung.com';
        $dis['base_url']=base_url();
        $dis['view']='front/includes/main_content';
		$this->viewfront($dis);
    }

    public function about(){
        $o = new Article(397);
        $dis['news'] = $o;

        $tag = $o->tag_vietnamese;
        $dis['tag'] = explode(',', $tag);

        // Customer hot in homepage
        $project_hot = new Article();
        $project_hot->order_by('created', 'desc');
        $project_hot->where(array('recycle'=>0, 'newscatalogue_id'=>67));
        $project_hot->get_iterated();
        $dis['project_hot'] = $project_hot;

        $this->page_title = $o->{'title_vietnamese'};
        $this->page_description = $o->short_vietnamese;
        $this->page_keyword = $o->tag;

        $this->menu_active = 'about';
        $dis['base_url'] = base_url();
        $this->uri = base_url().substr($this->uri->uri_string, 1, strlen($this->uri->uri_string));
        $dis['link'] = base_url().$_SERVER['REQUEST_URI'];
        $dis['view'] = 'front/singlepage';
        $this->viewfront($dis);
    }

    public function contact(){
        if($_SERVER['REQUEST_METHOD']=="POST"){

            $contact = new Contact();
            $contact->name = $this->input->post('name');
            $contact->address = $this->input->post('address');
            $contact->title = $this->input->post('title');
            $contact->email = $this->input->post('email');
            $contact->content = $this->input->post('content');
            /*$contact->phone = $this->input->post('phone');*/

            if($contact->save()) {

                $msg = "Cảm ơn bạn đã gửi thông tin. Chúng tôi sẽ phản hồi sớm nhất. Trân trọng !";
                $contact->clear();

            }
            else{

                $msg = "Có 1 số lỗi sau :\n";
                foreach($contact->error->all as $row){
                    $msg .= $row."\n";
                }
            }

        }
        $o = new Article(9);
        $dis['news'] = $o;

        $tag = $o->tag_vietnamese;
        $dis['tag'] = explode(',', $tag);

        $this->page_title = $o->{'title_vietnamese'};
        $this->page_description = $o->short_vietnamese;
        $this->page_keyword = $o->tag;

        $this->menu_active = 'contact';
        $dis['msg'] = $msg;
        $dis['base_url'] = base_url();
        $this->uri = base_url().substr($this->uri->uri_string, 1, strlen($this->uri->uri_string));
        $dis['link'] = base_url().$_SERVER['REQUEST_URI'];
        $dis['view'] = 'front/contact';
        $this->viewfront($dis);
    }

    function tags($title){
        $title = gettitlenonefromlink($this->uri->segment(2));

        $tags = explode('-', $title);
        $title = implode(' ', $tags);

        $news = new Article();
        $news->group_start();
        $news->like('tag_search', '%'.$title.'%');
        $news->where(array('recycle'=>0));
        $news->where_not_in('id', array(397, 9, 66));
        $news->group_end();
        $news->get();
        $dis['news'] = $news;


        // seo
        $this->page_title = $title.' | '.$this->page_title;
        $this->page_description = "Có ".$news->result_count()." kết quả tìm kiếm với từ khóa ".$title .' | '.$this->page_description;
        $this->page_keyword = $this->page_keyword;

        $this->column = 2;
        $dis['base_url']=base_url();
        $this->uri = base_url().substr($this->uri->uri_string, 1, strlen($this->uri->uri_string));
        $dis['view']='front/tags';

        $this->viewfront($dis);
    }

    function searchs(){

        $value = $this->input->post('value');

        $news = new Article();
        $news->group_start();
        $news->like('tag_search', '%'.$value.'%');
        $news->like('title_vietnamese', '%'.$value.'%');
        $news->where(array('recycle'=>0));
        $news->where_not_in('id', array(9, 367));
        $news->group_end();
        $news->get();
        $dis['news'] = $news;


        // seo
        $this->page_title = $value.' | '.$this->page_title;
        $this->page_description = "Có ".$news->result_count()." kết quả tìm kiếm với từ khóa ".$value .' | '.$this->page_description;
        $this->page_keyword = $this->page_keyword;

        $this->column = 2;
        $dis['base_url']=base_url();
        $this->uri = base_url().substr($this->uri->uri_string, 1, strlen($this->uri->uri_string));
        $dis['view']='front/search';

        $this->viewfront($dis);

    }
}
